function createCheckboxElements() {
	var ip = document.createElement("input");
	ip.setAttribute("type", "checkbox");
	return ip;
}

function createCateNode(arrCate, category) {
	var rootNode = $id("loadcategory");
	var nodeCateName = null;
	var nodeOption = null;
	var nodeDiv = null;
	var cate = null;
	var length = arrCate.length;
	rootNode.innerHTML = "";

	for (var i = 0; i < length; i++) {
		cate = arrCate[i];

		nodeOption = createNode("option");
		nodeOption.text = cate.categoryName;
		nodeOption.value = cate.categoryId;
		rootNode.appendChild(nodeOption);
	}
	//check category
	var opts = rootNode.options.length;
	for (var i = 0; i < length; i++) {
		if (rootNode.options[i].value == category) {
			rootNode.selectedIndex = i;
			break;
		}
	}
}

function createUpdateProductView(id, bookname, author, price, des, quan, urlImage) {
	$id("txtUpdateBookId").value = id;
	$id("txtUpdateBookName").value = bookname;
	$id("txtUpdateBookAuthor").value = author;
	$id("txtUpdateBookPrice").value = price;
	$id("txtUpdateBookDes").value = des;
	$id("txtUpdateBookQuantity").value = quan;
	$id("oldImg").innerHTML = urlImage;
	$id("txtUpdateBookImage").value = "";
	$id("btnUpdateProduct").setAttribute("onclick", "onclickbtnUpdateProduct()");
}

function onclickbtnUpdateProduct() {
	var bId = document.getElementById("txtUpdateBookId").value;
	var bName = document.getElementById("txtUpdateBookName").value;
	var category = document.getElementById("loadcategory").value;
	var author = document.getElementById("txtUpdateBookAuthor").value;
	var price = document.getElementById("txtUpdateBookPrice").value;
	var des = document.getElementById("txtUpdateBookDes").value;
	var quan = document.getElementById("txtUpdateBookQuantity").value;
	var imgUrl = document.getElementById("txtUpdateBookImage").value.replace("C:\\fakepath\\", "");
	if (validateBook(bName, category, author, price, des, quan)) {

		var file = $id("txtUpdateBookImage").files[0];

		var formData = new FormData();
		formData.append("file", file);
		formData.append("backendUrl", "http://localhost:8080/book-service/");
		formData.append("txtID", bId);
		formData.append("txtName", bName);
		formData.append("txtCategory", category);
		formData.append("txtAuthor", author);
		formData.append("txtPrice", price);
		formData.append("txtDes", des);
		formData.append("txtQuantity", quan);
		var xhr = new XMLHttpRequest();
		xhr.withCredentials = true;

		xhr.onreadystatechange = function() {
			if (this.readyState == 4) {
				if (this.status == 200) {

					alert("Update sách có tên " + bName + " thành công!");
					showPage("content-manageProduct");
					callProductListService();

				} else if (this.status == 401) {
					alert("Bạn không có quyền truy cập vào trang này!\n Xin mời đăng nhập lại");
					doRequestFail();
				} else {
					alert("Có lỗi xảy ra. Xin thử lại!");
				}
			}
		}

		xhr.open('PUT', "http://localhost:8080/book-service/staff/manageBook", false);

		xhr.send(formData);

	}
}